<?php

namespace StudyBuddy;

/**
 *
 * Helper class for presenting
 * the block with user's answers
 * on the profile page
 *
 * This class can also be used when we need to see all
 * user answers like on administation page
 */
class UserAnswers extends StudyBuddyObject {
    const PER_PAGE = 10;

    public static function get(Registry $oRegistry, User $oUser) {

        $uid = $oUser->getUid();
        if (0 === $uid) {
            d('not registered user');

            return '';
        }

        $pagerLinks = '';
        /**
         * Default pager path
         */
        $pagerPath = '/tab/a/' . $uid . '/oldest';

        $cond = $oRegistry->Request->get('sort', 's', 'recent');
        switch ($cond) {
            case 'oldest':
                $sort = array('_id' => 1);
                $pagerPath = '/tab/a/' . $uid . '/recent';
                break;

            case 'voted':
                $sort = array('i_votes' => -1);
                $pagerPath = '/tab/a/' . $uid . '/voted';
                break;

            case 'updated':
                $sort = array('i_lm_ts' => -1);
                $pagerPath = '/tab/a/' . $uid . '/updated';
                break;


            case 'best':
                $sort = array('accepted' => -1);
                $pagerPath = '/tab/a/' . $uid . '/best';
                break;
            default:
                $sort = array('_id' => -1);
                $pagerPath = '/tab/a/' . $uid . '/oldest';
                break;
        }

        $cursor = self::getCursor($oRegistry, $uid, $sort);
        $count = $cursor->count(true);
        d('$count: ' . $count);

        /**
         * If this user does not have any answers then return
         * empty string, skip any unnecessary template parsing
         */
        if (0 == $count) {
            d('no user answers');
            return '';
        }

        $pageID = $oRegistry->Request->get('pageID', 'i', 1);

        if ($count > self::PER_PAGE || $pageID > 1) {
            $oPaginator = Paginator::factory($oRegistry);
            $oPaginator->paginate($cursor, self::PER_PAGE, array('path' => $pagerPath));

            $pagerLinks = $oPaginator->getLinks();
            d('$pagerPath: ' . $pagerPath . ' pagerLinks: ' . $pagerLinks);
        }

        $answers = \tplUanswers::loop($cursor);
        d('$answers: ' . $answers);

        $vals = array(
            'count' => $count,
            'answers' => $answers,
            'pagination' => $pagerLinks);

        return \tplUserAnswers::parse($vals);
    }

    /**
     *
     * Get result of selectin answers from ANSWERS collection
     *
     * @param Registry $oRegistry
     * @param int $uid
     *
     * @return object MongoCursor
     *
     * @todo use different 'sort' params based on
     * what's passed in Request: by votes,
     * or by creation date
     */
    protected static function getCursor(Registry $oRegistry, $uid, array $sort) {
        $where = array('i_uid' => $uid);
        /**
         * Exclude deleted items unless viewer
         * is a moderator
         */
        if (!$oRegistry->Viewer->isModerator()) {
            $where['i_del_ts'] = null;
        }

        $cursor = $oRegistry->Mongo->ANSWERS->find($where)->sort($sort);

        return $cursor;
    }

}
